package cn.fl.institution.controller;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.customer.service.ICstNpBscInfoService;
import cn.fl.customer.vo.CstNpBscInfoVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Description: TODO 三井住友
 * @Author yangzhongkun
 * @Date 2020/5/21 12:28
 * @Version V1.0
 **/
@Controller
@RequestMapping("/institution/smbc")
public class PrjBscInfoInsSmbcController {

    /**
     * 自然人信息
     */
    @Autowired
    private ICstNpBscInfoService cstNpBscInfoService;

    /**
     * 字典项查询
     */
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;

    @RequestMapping(value = "/save.do", method = RequestMethod.POST)
    @ResponseBody
    public DataResultVO save(HttpServletRequest request) throws BaseException {

        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "保存成功");
        //客户id
        String cstId = request.getParameter("cstId");
        //学历
        String highDgCd = request.getParameter("highDgCd");
        if(StringUtils.isEmpty(highDgCd)){
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("最高学历不能为空");
            return result;
        }
        CstNpBscInfoVO npBscInfoVO = new CstNpBscInfoVO();

        //根据客户id查询 客户信息
        CstNpBscInfoVO cstNpBscInfoVO = this.cstNpBscInfoService.selectById(Long.valueOf(cstId));
        if(null != cstNpBscInfoVO && !Objects.equals(highDgCd,cstNpBscInfoVO.getHighDgCd())){

            npBscInfoVO.setId(cstNpBscInfoVO.getId());
            npBscInfoVO.setHighDgCd(highDgCd);

            //查询字典项
            BaseLogUtils.newLogger("save.三井住友查询最高学历字典项Start:highDgCd={}" ,highDgCd).info();
            List<KeyValueVO> highDgCdList = this.sysDictionaryApiService.getKvList(DictionaryConstant.CODE_GRP_CST_HIGH_DG_CD, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
            BaseLogUtils.newLogger("save.三井住友查询最高学历字典项End:highDgCdList={}" , JSONObject.toJSONString(highDgCdList)).info();

            for (KeyValueVO keyValueVO:highDgCdList) {
                if(highDgCd.equals(keyValueVO.getKey())){
                    npBscInfoVO.setHighDgCdNm(keyValueVO.getValue());
                }
            }

            npBscInfoVO.setMdfTm(new Date());//更新时间
            npBscInfoVO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());//更新人

        }

        BaseLogUtils.newLogger("save.三井住友修改自然人信息Start:npBscInfoVO={}" , JSON.toJSONString(cstNpBscInfoVO)).info();
        boolean modify = this.cstNpBscInfoService.modify(npBscInfoVO);
        BaseLogUtils.newLogger("save.三井住友修改自然人信息Start:npBscInfoVO={}" , modify).info();

        return result;
    }
}
